<?php

class Ecard_AdminImageController extends Core_Controller_Action_Admin 
{
    public function indexAction() {
		
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
      		->getNavigation('ecard_admin_main', array(), 'ecard_admin_main_image');
		
//      $this->view->formFilter = $formFilter = new Webmix_Form_Admin_Company_Filter();
	    $page = $this->_getParam('page', 1);

	    $table = Engine_Api::_()->getDbtable('images', 'ecard');
	    $select = $table->select();

    
        // Make paginator
        $this->view->paginator = $paginator = Zend_Paginator::factory($select);
        $paginator->setItemCountPerPage(150);
        $this->view->paginator = $paginator->setCurrentPageNumber( $page );

  }
  public function createAction()
  {
	if( !$this->_helper->requireUser()->isValid() ) {
		return;
	}
    
    $this->view->form = $form = new Ecard_Form_Admin_Image_Create();
    
    // Check post
    if(!$this->getRequest()->isPost()){
        return;
    }
    // Check form validate
    if(!$form->isValid($this->getRequest()->getPost())){
        return;
    }
    $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer();

    $values = $form->getValues();
   
    //refresh photo when upload a new one
    if($values['file_id']){
        $photo = Engine_Api::_()->getItemTable('storage_file')->getFile($values['file_id'],'thumb.normal');                            
        $photo_url = $photo->map();
        $this->view->photo_url = $photo_url;               
    }     
    
    $table = Engine_Api::_()->getDbTable('images','ecard');
        
    //save form values
    $db = $table->getAdapter();
    $db->beginTransaction();
    try
    {            
        $image = $table->createRow();
        $image->setFromArray($values);
        $image->status = 1;
        $image->owner_id = $viewer->getIdentity();
        $image->photo_id = $values['file_id'];
        $image->save();
        $db->commit();   
        $form->addNotice($this->view->translate('Your company was created successfully.'));                  
            
    }
    catch(Exception $e)
    {
        $db->rollBack();
        throw $e;
    }        
          
    //return admin company after create
    return $this->_helper->redirector->gotoRoute(array('module'=>'ecard','controller'=>'image'), 'admin_default', true);
          
  }
  
  public function previewAction(){
        
        $table = Engine_Api::_()->getDbTable('images','ecard'); 
        $file_id = $table->setPhoto($_FILES['image_upload']); 
        $type = 'thumb.normal';
        
        $preview = Engine_Api::_()->getItemTable('storage_file')->getFile($file_id, $type);
            if( !$preview ) {
                return ;
            } 
        $file = Engine_Api::_()->getItemTable('storage_file')->getFile($file_id);
        
        //get full size photo to save         
        $file_url = $file->map();
        $this->view->file_url = $file_url;
        $this->view->file_id = $file_id;
        

        //get normal photo to preview   
        $preview_url = $preview->map();
        $this->view->preview_url = $preview_url;    
                            
  } 


  public function editAction(){
    	if( !$this->_helper->requireUser()->isValid() ) {
    		return;
    	}     
        
        //get company_id when edit in Admin
        $image_id = $this->_getParam('image');
        //get photo_id when upload image in admin edit
                
        $this->view->form = $form = new Ecard_Form_Admin_Image_Edit();
                
        $image = Engine_Api::_()->getItem('ecard_image',$image_id);    
        $form->populate($image->toArray());
          
        // Check post form
        if( !$this->getRequest()->isPost() ) {
          return;
        }
        if( !$form->isValid($this->getRequest()->getPost()) ) {
          return;
        }
        
        $values = $form->getValues();    
        
        //refresh photo when upload a new one
        if($values['file_id']){
            $photo = Engine_Api::_()->getItemTable('storage_file')->getFile($values['file_id'],'thumb.normal');                            
            $photo_url = $photo->map();
            $this->view->photo_url = $photo_url;               
        }
            
        // Process
        $db = Engine_Db_Table::getDefaultAdapter();
        $db->beginTransaction();        
        
        try{
            $image->setFromArray($values);
            $image->photo_id = $values['file_id'];    

            $image->save();
            $db->commit();   
            
            return $this->_forward('success', 'utility', 'core', array(
            'smoothboxClose' => 1000,
            'parentRefresh' => 10,
            'format'=> 'smoothbox',
            'messages' => array('Your changes have been saved.')
            ));
            
        }
        catch(Exception $e)
        {
            $db->rollback();
            throw $e;
        }
    }
    public function approveAction()
  {
    $id = $this->_getParam('image', null);
    $approve = $this->_getParam('approve', 1);
    $this->view->ecard = $ecard = Engine_Api::_()->getItem('ecard_image', $id);
    $this->view->form = $form = new Ecard_Form_Admin_Image_Approve();
    $viewer = Engine_Api::_()->user()->getViewer();
    if(!$approve) $form->setTitle('Disapprove Image')
    					->setDescription('Are you sure you want to disapprove this image?');
    // deleting user
    //$form->user_id->setValue($id);

    if( $this->getRequest()->isPost() ) {
      $db = Engine_Api::_()->getDbtable('images', 'ecard')->getAdapter();
      $db->beginTransaction();

      try {
        $ecard->approve = $approve;
        $ecard->save();
        
        $db->commit();
      } catch( Exception $e ) {
        $db->rollBack();
        throw $e;
      }
      
      return $this->_forward('success', 'utility', 'core', array(
        'smoothboxClose' => true,
        'parentRefresh' => true,
        'format'=> 'smoothbox',
        'messages' => array('This image has been successfully changed.')
      ));
    }
  }
  
  public function deleteAction()
  {
        $id = $this->_getParam('id', null);
        $this->view->image = $image = Engine_Api::_()->getItem('ecard_image', $id);
        $this->view->form = $form = new Ecard_Form_Admin_Image_Delete();
        $viewer = Engine_Api::_()->user()->getViewer();

        if( !$this->getRequest()->isPost() ) {
            return;
        }   
    
        $db = Engine_Api::_()->getDbtable('images', 'ecard')->getAdapter();
        $db->beginTransaction();
        try {                        
            $image->delete();        
            $db->commit();
        } catch( Exception $e ) {
            $db->rollBack();
        throw $e;
        }
              
        return $this->_forward('success', 'utility', 'core', array(
            'smoothboxClose' => true,
            'parentRefresh' => true,
            'format'=> 'smoothbox',
            'messages' => array('This image has been successfully deleted.')
        ));       
  }
}
